Skip to content

Music copies are one play; backups carry no tokens; LAN apps keep their origin - #363

Merged
Randomname653 merged 8 commits into
mainfrom
claude/music-copies
Nov 15, 2026
Merged

Randomname653 merged 8 commits into
mainfrom
claude/music-copies

Conversation

@Randomname653

@Randomname653 Randomname653 commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Three fixes found after #358 brought the owner's Plexamp history in.

1. One song played is one play.
Two causes, both measured on a fresh backup of the owner's install:

  • Plex gives every copy of a track in the library (the album, the remaster, a compilation) the same lastViewedAt second, while its history holds one play. Example: "Lovely Day" three times at 00:46:51, one of the copies without genres.
  • A song stored from the listing and from the history landed twice. The two stamps were 147 s apart (the song's two ends), and the 90-second same-play rule was made for video.

The fix:

  • plex_sync.drop_music_copies keeps one listing entry per song and second, the one with a genre. It runs before the three-in-a-second mark rule counts.
  • _same_play_s: a song's same-play window is its length. A history event without a length gets 6 minutes. Video keeps 90 s.
  • A history event on another copy of a listed song counts as the same play.
  • The schema 4 migration keeps one row per song and second, preferring one with genres that counts as a play (not marked). It also drops a second row of the same song that falls inside the first one's length.

On a copy of the owner's real backup:

watch_history rows before after
music from Plex 48,258 40,204
music marked 628 105
music from Spotify, video unchanged

In 137 groups only marked copies were stored; their kept row now counts as the play it was. The taste profile had counted all the copies.

2. Backups: no tokens inside, 0600 by default, the cache snapshot opt-in.
This follows a look at how comparable self-hosted apps handle backups (Sonarr, Tautulli, Plex, Home Assistant, Immich, Jellyfin). They back up only what cannot be rebuilt, the database and config. They skip caches, keep files 0600, and Plex keeps its token out of its database backups.

  • No tokens in the backup. scheduler._strip_secrets blanks users.plex_token and the session tokens in the backup copy, with secure_delete on so the bytes are overwritten. It leaves the copy as one file and checks it again.
    • A restore asks each member to sign in once; the token is refreshed at every sign-in anyway.
    • The server's own token lives in .env, never in the database.
  • Modes. Backups are 0600/0700, like the database.
    • BACKUP_GROUP_READABLE=true makes them 0640/0750, for a NAS user in the app's group.
    • The start-up pass used to force every backup to 0600, so a manual chmod was gone after the next restart. It now follows this setting.
    • The live database and .env stay 0600.
  • BACKUP_CACHES, off by default. The cache and vector-store snapshot that shipped in 1.2.0 runs only when asked for. When the setting is off, an existing snapshot (~2 GB) is removed. The demo builder names both settings when they are missing.
  • Both settings live in .env only, documented in .env.example.

3. LAN apps keep to their own origin, port included.

CHANGELOG Unreleased covers these fixes and #360–#362.

Tests: 357/357.

  • test_music_copies (new, 11 checks): sync, history match, migration.
  • test_backup_perms (new): token bytes gone from the backup file while the live DB keeps them; modes per setting (POSIX, checked in CI); cache snapshot off, on, then removed.
  • test_backup_retention +1: a new snapshot is 0600 by default.
  • test_fresh_install +2: another port is refused; an icon URL with a query is fetched.
  • test_single_marks now pins "generation 3 or later".

After deploying: the migration runs once at start. The nightly backup from before stays as a fallback.

🤖 Generated with Claude Code

Randomname653 and others added 7 commits November 15, 2026 09:17
- A LAN app's logo fetch stays on that app's origin - scheme, host AND
  port: one NAS answers for a dozen apps on one address, a redirect to
  another port is another app (the same-origin idea from Sentinel #356).
- The endpoint policy judges the app's address once instead of every hop:
  it is for base URLs and refused an icon link with a query, so SoulSync
  (/static/pwa-icon-192.png?v=...) showed no logo since #353.

Live: Radarr, Sonarr and SoulSync logos from the LAN all arrive.
test_fresh_install +2 (another port refused; a query icon fetched).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… every start

The start-up pass set every SQLite file under data/ to 0600, the backups
included: the backup written at 22:47 could be copied off the NAS share
until the 23:43 restart turned it 0600 (2026-11-14), and a manual chmod
would be undone by the next start. Now:
- _secure_db_files gives files under data/backups 0640 (BACKUP_MODE);
- the nightly DB backup writes its snapshot 0640;
- the cache snapshot is written 0640, its folders 0750.
The live database, its sidecars, the caches, the vector store and .env
stay 0600 (a live SQLite file is never copied; .env holds the tokens).

tests/test_backup_perms.py (POSIX modes, checked in CI) and
test_backup_retention +1 (a new snapshot is 0640).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reading the owner's music library whole stored copies: Plex gives every
copy of a track (album, remaster, compilation) the same lastViewedAt second
while its history holds one play ('Lovely Day' three times at 00:46:51,
one of them without genres), and a song stored from the listing and from
the history landed twice, the two stamps 147 s apart (the 90-second
same-play rule is for video).

- plex_sync.drop_music_copies: per song and second one listing entry stays
  (the one with a genre), before the three-in-a-second mark rule counts.
- _same_play_s: a song's same-play window is its length (a history event
  without a length: 6 minutes); video keeps 90 s. Used where a history
  event meets the listing and the stored plays.
- A history event of another copy of a listed song is that same play.
- Schema 4 migration: per song and second one row stays (with genres, a
  play rather than 'marked'), and a second row of a song inside the first
  one's length goes. On a copy of the owner's backup: music plays from
  Plex 48,258 -> 40,204, 'marked' 628 -> 105 (137 groups of only marked
  copies now count as the play they were); Spotify, films, shows and anime
  untouched.

tests/test_music_copies.py (11 checks: sync, history match, migration);
test_single_marks pins 'generation 3 or later'.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
#362 followed an arr's login redirect by hand on the asked host; one NAS
answers for a dozen apps, one per port. The probe now uses the LAN logo
fetch's check (setup_icons._lan_ok): same scheme, host and port, the
endpoint policy judging the app's address once. Live: Radarr, Sonarr and
SoulSync found as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/services/cache_snapshot.py Fixed
Reworked after a look at how comparable self-hosted apps do it (Sonarr,
Tautulli, Plex, Home Assistant, Immich, Jellyfin): they back up what
cannot be rebuilt (database, config), skip caches, keep 0600, and Plex
keeps its token out of its database backups.

- scheduler._strip_secrets: the backup copy's users.plex_token (and the
  session tokens) are blanked with secure_delete on, the copy left one
  file and checked again. A restore asks each member to sign in once (the
  token is refreshed at every sign-in); the server's token lives in .env.
- Modes: backups 0600/0700 like the database (connection.backup_modes);
  BACKUP_GROUP_READABLE=true makes them 0640/0750 for a NAS user in the
  app's group, and the start-up pass follows the setting instead of forcing
  0600. The live database and .env stay 0600.
- BACKUP_CACHES (default false): the cache and vector-store snapshot from
  1.2.0 only when asked for; switched off, a snapshot already written is
  removed. The demo builder names both settings when they are missing.
- Both settings env-only (.env.example documents them).

tests/test_backup_perms.py reworked (tokens gone from the file bytes, live
DB keeps them, modes per setting, cache snapshot off/on/removed);
test_backup_retention expects 0600; test_cache_snapshot turns the option on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Randomname653 Randomname653 changed the title Music copies are one play; backups stay copyable; LAN apps keep their origin Music copies are one play; backups carry no tokens; LAN apps keep their origin Nov 15, 2026
@Randomname653
Randomname653 merged commit ca16939 into main Nov 15, 2026
9 checks passed
@Randomname653
Randomname653 deleted the claude/music-copies branch November 15, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants